package com.zy.qudadid.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.services.core.PoiItem;
import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.InputtipsQuery;
import com.amap.api.services.help.Tip;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.zy.adapter.DizhiAdapter;
import com.zy.adapter.RecomandAdapter;
import com.zy.base.BaseActivity;
import com.zy.bean.Didian;
import com.zy.bean.PositionEntity;
import com.zy.cityList.CityPickerActivity;

import com.zy.lcpassenger.R;
import com.zy.gaode.PoiSearchTask;
import com.zy.gaode.RouteTask;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2016/8/2 0002.
 */
public class DidianActivity extends BaseActivity implements TextWatcher, AdapterView.OnItemClickListener, PoiSearch.OnPoiSearchListener,Inputtips.InputtipsListener {

    TextView back,city_adds,home,com;
    ListView dizhi;
    DizhiAdapter dizhiAdapter;
    EditText didian;
    private RecomandAdapter mRecomandAdapter;
    private RouteTask mRouteTask;
    ArrayList<Didian> didianArrayList = new ArrayList<>();
    String address,biaoshi,city;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_didianliebiao);
        didian = (EditText) findViewById(R.id.didian);
        dizhi = (ListView) findViewById(R.id.dizhi);
        city_adds = (TextView) findViewById(R.id.city_adds);
        home = (TextView) findViewById(R.id.home);
        com = (TextView) findViewById(R.id.com);
        biaoshi = getIntent().getStringExtra("biaoshi");
        if(biaoshi.equals("kaishi")){
            didian.setHint("您在哪儿上车");
        }else if(biaoshi.equals("xiache")){
            didian.setHint("您在哪儿下车");
        }
        dizhiAdapter = new DizhiAdapter(DidianActivity.this,didianArrayList,biaoshi);
        didian.addTextChangedListener(this);
        address = getIntent().getStringExtra("address");
        city = getIntent().getStringExtra("citycz");
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(this);
        dizhi.setOnItemClickListener(this);
        dizhi.setAdapter(dizhiAdapter);
        city_adds.setText(city);
        mRouteTask=RouteTask.getInstance(getApplicationContext());
        city_adds.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(DidianActivity.this,CityPickerActivity.class),1023);
            }
        });

//        final User u = new UserUtil(this).getUser();
//        if (StringUtil.isEmpty(u.home_addr)){
//            home.setText("设置家的地址");
//        }else{
//            home.setText(u.home_addr);
//        }
//        if (StringUtil.isEmpty(u.com_addr)){
//            com.setText("设置公司地址");
//        }else{
//            com.setText(u.com_addr);
//        }

//        home.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (home.getText().toString().equals("设置家的地址")){
////                    Toast.makeText(getApplicationContext(), "请到侧边菜单-设置里选择常用地址", Toast.LENGTH_SHORT).show();
//
//                    Intent intent = new Intent(DidianActivity.this,ChangyongdizhiActivity.class);
//                    startActivity(intent);
//                }else{
//                    if(biaoshi.equals("kaishi")) {
//                        MainActivity.shangche = u.home_addr;
//                        MainActivity.shangchelat = u.home_addr_lat;
//                        MainActivity.shangchelng = u.home_addr_lng;
//                    }else{
//                        MainActivity.quna = u.home_addr;
//                        MainActivity.qunalat = u.home_addr_lat;
//                        MainActivity.qunalng = u.home_addr_lng;
//                    }
//                    MainActivity.isshow = "no";
//                    finish();
//                }
//            }
//        });
//        com.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (com.getText().toString().equals("设置公司地址")){
////                    Toast.makeText(getApplicationContext(), "请到侧边菜单-设置里选择常用地址", Toast.LENGTH_SHORT).show();
//                    Intent intent = new Intent(DidianActivity.this,ChangyongdizhiActivity.class);
//                    startActivity(intent);
//                }else{
//                    if(biaoshi.equals("kaishi")) {
//                        MainActivity.shangche = u.com_addr;
//                        MainActivity.shangchelat = u.com_addr_lat;
//                        MainActivity.shangchelng = u.com_addr_lng;
//                    }else {
//                        MainActivity.quna = u.com_addr;
//                        MainActivity.qunalat = u.com_addr_lat;
//                        MainActivity.qunalng = u.com_addr_lng;
//                    }
//                    MainActivity.isshow = "no";
//                    finish();
//                }
//            }
//        });

        searchTip();

    }

    private void seach(){
//        didianArrayList.clear();
        if(RouteTask.getInstance(getApplicationContext()).getStartPoint()==null){
            Toast.makeText(getApplicationContext(), "检查网络，Key等问题", Toast.LENGTH_SHORT).show();
            return;
        }
        PoiSearch.Query query = new PoiSearch.Query(address,"",city);
        query.setPageSize(10);
        query.setPageNum(1);
        PoiSearch poiSearch = new PoiSearch(this,query);
        poiSearch.setOnPoiSearchListener(this);
        poiSearch.searchPOIAsyn();
    }

    private void searchTip(){
        //第二个参数默认代表全国，也可以为城市区号
        InputtipsQuery inputquery = new InputtipsQuery(address, city);
        inputquery.setCityLimit(true);
        Inputtips inputTips = new Inputtips(DidianActivity.this, inputquery);
        inputTips.setInputtipsListener(this);
        inputTips.requestInputtipsAsyn();
    }

    @Override
    public void onClick(View v){
        super.onClick(v);
        switch (v.getId()){
            case R.id.back:
//                setResult(0,new Intent());
                finish();
                break;
        }
    }

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
////            setResult(0,new Intent());
//            finish();
//            return true;
//        }
//        return super.onKeyDown(keyCode, event);
//    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        //第二个参数默认代表全国，也可以为城市区号
        InputtipsQuery inputquery = new InputtipsQuery(s.toString(), city);
        inputquery.setCityLimit(true);
        Inputtips inputTips = new Inputtips(DidianActivity.this, inputquery);
        inputTips.setInputtipsListener(this);
        inputTips.requestInputtipsAsyn();
    }

    @Override
    public void afterTextChanged(Editable s) {

        //第二个参数默认代表全国，也可以为城市区号
        InputtipsQuery inputquery = new InputtipsQuery(s.toString(), city);
        inputquery.setCityLimit(true);
        Inputtips inputTips = new Inputtips(DidianActivity.this, inputquery);
        inputTips.setInputtipsListener(this);
        inputTips.requestInputtipsAsyn();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        PositionEntity entity = (PositionEntity) mRecomandAdapter.getItem(position);
        if (entity.latitue == 0 && entity.longitude == 0) {
            PoiSearchTask poiSearchTask=new PoiSearchTask(getApplicationContext(), mRecomandAdapter);
            poiSearchTask.search(entity.adname,RouteTask.getInstance(getApplicationContext()).getStartPoint().city);

        } else {
            mRouteTask.setEndPoint(entity);
            mRouteTask.search();
//            Intent intent = new Intent(DestinationActivity.this, MainActivity.class);
//            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
//            startActivity(intent);
//            finish();
        }
    }

    int i =0;
    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {

        for(i=0;i<poiResult.getPois().size();i++){
            Log.i("GGGG","onPoiSearched== title "+poiResult.getPois().get(i).getTitle()+" getProvinceName "+poiResult.getPois().get(i).getProvinceName()+" getSubPois " +poiResult.getPois().get(i).getSubPois()+" getCityName "+poiResult.getPois().get(i).getCityName()+" getAdName "+poiResult.getPois().get(i).getAdName()+" getDirection "+poiResult.getPois().get(i).getDirection()+" getParkingType "+poiResult.getPois().get(i).getParkingType()+" getSnippet "+poiResult.getPois().get(i).getSnippet()+" getTypeDes "+poiResult.getPois().get(i).getTypeDes()+" getSnippet "+poiResult.getPois().get(i).getSnippet()+" getParkingType "+poiResult.getPois().get(i).getParkingType());
            didianArrayList.add(new Didian(poiResult.getPois().get(i).getLatLonPoint().getLatitude()+"",poiResult.getPois().get(i).getLatLonPoint().getLongitude()+"",poiResult.getPois().get(i).getCityName()+poiResult.getPois().get(i).getAdName()+poiResult.getPois().get(i).getSnippet(),poiResult.getPois().get(i).getTitle(),poiResult.getPois().get(i).getCityName()));
        }
        dizhiAdapter = new DizhiAdapter(DidianActivity.this,didianArrayList,biaoshi);
        dizhi.setAdapter(dizhiAdapter);
        dizhi.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(biaoshi.equals("kaishi")){
                    MainActivity.shangche = didianArrayList.get(position).getAdname();
                    MainActivity.shangchelat = didianArrayList.get(position).getLat();
                    MainActivity.shangchelng = didianArrayList.get(position).getLng();

//                    Intent it = new Intent();
//                    Bundle bundle = new Bundle();
//                    bundle.putString("name",didianArrayList.get(position).getAdname());
//                    bundle.putString("lat",didianArrayList.get(position).getLat());
//                    bundle.putString("lng",didianArrayList.get(position).getLng());
//                    it.putExtra("data",bundle);
//                    setResult(1,it);

                    finish();
                }else{
                    MainActivity.quna = didianArrayList.get(position).getAdname();
                    MainActivity.qunalat = didianArrayList.get(position).getLat();
                    MainActivity.qunalng = didianArrayList.get(position).getLng();
                    MainActivity.isshow = "no";

//                    Intent it = new Intent();
//                    Bundle bundle = new Bundle();
//                    bundle.putString("name",didianArrayList.get(position).getAdname());
//                    bundle.putString("lat",didianArrayList.get(position).getLat());
//                    bundle.putString("lng",didianArrayList.get(position).getLng());
//                    it.putExtra("data",bundle);
//                    setResult(2,it);

                    finish();
                }
            }
        });
    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {
        for(i=0;i<poiItem.getSubPois().size();i++){
            Log.i("GGGG","onPoiSearched=="+poiItem.getSubPois().get(i).getTitle());
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1023&&resultCode == -1){
            city = data.getStringExtra("picked_city");
//            cityid = data.getStringExtra("picked_city");
            city_adds.setText(city);
        }
    }

    @Override
    public void onGetInputtips(List<Tip> tipList, int rCode) {
        if (rCode == 1000) {
            List<HashMap<String, String>> listString = new ArrayList<HashMap<String, String>>();
            didianArrayList.clear();
            for (int i = 0; i < tipList.size(); i++) {
                Tip item = tipList.get(i);
                if(!TextUtils.isEmpty(item.getDistrict())) {
                    didianArrayList.add(new Didian(tipList.get(i).getPoint().getLatitude() + "", tipList.get(i).getPoint().getLongitude() + "", tipList.get(i).getDistrict() + "", tipList.get(i).getName(), city));
                }
            }
            dizhiAdapter = new DizhiAdapter(DidianActivity.this,didianArrayList,biaoshi);
            dizhi.setAdapter(dizhiAdapter);
            dizhi.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if(biaoshi.equals("kaishi")){
                        MainActivity.shangche = didianArrayList.get(position).getAdname();
                        MainActivity.shangchelat = didianArrayList.get(position).getLat();
                        MainActivity.shangchelng = didianArrayList.get(position).getLng();


                        finish();
                    }else{
                        MainActivity.quna = didianArrayList.get(position).getAdname();
                        MainActivity.qunalat = didianArrayList.get(position).getLat();
                        MainActivity.qunalng = didianArrayList.get(position).getLng();
                        MainActivity.isshow = "no";

                        finish();
                    }
                }
            });
        }
    }
}
